/*
 * Copyright (C) 2015 Tomás Ruiz-López.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chang.jason.recyclerviewinrecyclerview.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chang.jason.recyclerviewinrecyclerview.R;
import com.chang.jason.recyclerviewinrecyclerview.viewholders.CountFooterViewHolder;
import com.chang.jason.recyclerviewinrecyclerview.viewholders.CountHeaderViewHolder;
import com.chang.jason.recyclerviewinrecyclerview.viewholders.CountItemViewHolder;
import com.chang.jason.recyclerviewinrecyclerview.viewholders.SectionedRecyclerViewAdapter;
//import com.truizlop.sectionedrecyclerview.SectionedRecyclerViewAdapter;


public class CountSectionAdapter extends SectionedRecyclerViewAdapter<CountHeaderViewHolder,
        CountItemViewHolder,
        CountFooterViewHolder> {

    protected Context context = null;

    public CountSectionAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected int getItemCountForSection(int section) {//每一个条目对应的子项数
        return section + 1;
    }

    @Override
    protected int getSectionCount() {//一共多少条目
        return 5;
    }

    @Override
    protected boolean hasFooterInSection(int section) {//对应条目是否有footer
        return true;
    }

    protected LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(context);
    }

    //创建header
    @Override
    protected CountHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.view_count_header, parent, false);
        return new CountHeaderViewHolder(view);
    }

    //创建footer
    @Override
    protected CountFooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.view_count_footer, parent, false);
        return new CountFooterViewHolder(view);
    }

    //创建item
    @Override
    protected CountItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.view_count_item, parent, false);
        return new CountItemViewHolder(view);
    }

    //绑定header
    @Override
    protected void onBindSectionHeaderViewHolder(CountHeaderViewHolder holder, int section) {
        holder.render("Section " + (section + 1));
    }

    //绑定footer
    @Override
    protected void onBindSectionFooterViewHolder(CountFooterViewHolder holder, int section) {
        holder.render("Footer " + (section + 1));
    }

    //绑定item
    protected int[] colors = new int[]{0xfff44336, 0xff2196f3, 0xff009688, 0xff8bc34a, 0xffff9800};

    @Override
    protected void onBindItemViewHolder(CountItemViewHolder holder,final int section,final int position) {
        holder.render(String.valueOf(position + 1), colors[section]);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("*******","section="+section+";;position="+position);
            }
        });
    }
}
